import { readdirSync, statSync, writeFileSync, readFile } from 'fs';
import { resolve } from 'path';
let searchFile = (dir, ext) => {
	let arr = [];
	(function loop(dir, ext) {
		readdirSync(dir).forEach((name, index) => {
			const curPath = resolve(dir, name);
			(new RegExp('\\.' + ext + '$').test(name)) ? arr.push(curPath) : (statSync(curPath).isDirectory() && loop(curPath, ext));
		})
	})(dir, ext);
	return arr;
}
let  replaceWriteFile = (reg,text,data,filePath) => {
    const res=[]
    var reg=eval(reg)
	res.push(data.match(reg))
	res && res.forEach((item ,index) => {
    	if(item!=null){
    		var reText = data.replace(reg,text) 
    		writeFileSync(filePath, reText, 'utf-8');
            console.log('上线文件替换为:'+ text);
    	}
    }) 
}
searchFile(__dirname+"/lib/", 'js').forEach((filePath, index) => {
  	readFile(filePath, "utf8", function(err, data){  
        if(!err){
        	//替换DZonline=false为 var DZonline=true
        	replaceWriteFile(/DZonline=false/gim,"DZonline=true",data,filePath);
        	//替换https://blue-m.weizhipin.com 为apiHost=""
        	replaceWriteFile(/hostapi=\"https:\/\/blue-boss.weizhipin.com\"/gim,"hostapi=\"https:\/\/boss.dianzhangzhipin.com\"",data,filePath);
        }  
     }); 
  })

